package com.yzz.composeweatherproject.activity

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import coil.compose.AsyncImage
import coil.compose.rememberAsyncImagePainter
import coil.compose.rememberImagePainter
import coil.request.ImageRequest
import com.yzz.composeweatherproject.IDataSuccessListener
import com.yzz.composeweatherproject.activity.ui.theme.ComposeWeatherProjectTheme
import com.yzz.composeweatherproject.entity.WeatherCityBean
import com.yzz.composeweatherproject.http.okhttp.OkHttpUtils
import com.yzz.composeweatherproject.viewmodel.DataViewModel

class WeatherinDetailActivity : ComponentActivity() {
    private val mViewModel by viewModels<DataViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeWeatherProjectTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    val theIntent = intent
                    theIntent?.let { intent ->
                        intent.getBundleExtra("bundle")?.let { bun ->
                            bun.getSerializable("data")?.let { data ->
                                val bean: WeatherCityBean? = data as WeatherCityBean?
                                mViewModel.getWeatherDetailed(bean?.url)
                            }
                        }
                    }
                }
            }
            Greeting3(mViewModel)

        }
    }
    @Composable
    fun Greeting3(viewmodel: DataViewModel) {
        val data = viewmodel.detailedDataFlow.collectAsState()
        val bean = data.value ?: return
        //加载图片的 rememberAsyncImagePainter
        val painter = rememberAsyncImagePainter(
            model = ImageRequest.Builder(LocalContext.current)
                .data(bean.cityImgUrl)
                .crossfade(true)
                .build(),
            contentScale = ContentScale.Crop
        )
        ConstraintLayout(modifier = Modifier.fillMaxWidth()) {
            val (img, nameText, timeText,
                temperature, weatherState, humidityText) = createRefs()
            Image(
                painter = painter,
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier.constrainAs(img) {
                    top.linkTo(parent.top)
                    start.linkTo(parent.start)
                    width = Dimension.percent(0.3f)
                }
            )

            Text(text = bean.name, modifier = Modifier.constrainAs(nameText) {
                start.linkTo(img.end)
                top.linkTo(img.top)
            })
            Text(text = bean.time, modifier = Modifier.constrainAs(timeText) {
                top.linkTo(nameText.bottom)
                start.linkTo(nameText.start)
            })
            Text(text = bean.temperature, Modifier.constrainAs(temperature) {
                top.linkTo(img.bottom)
                start.linkTo(img.start)
            })
            Text(text = bean.weatherState, Modifier.constrainAs(weatherState) {
                start.linkTo(temperature.end)
                top.linkTo(temperature.top)
            })
            Text(text = bean.humidity, Modifier.constrainAs(humidityText) {
                start.linkTo(temperature.start)
                top.linkTo(temperature.bottom)
            })
        }

    }

    @Preview(showBackground = true)
    @Composable
    fun DefaultPreview3() {
        ComposeWeatherProjectTheme {
//        Greeting3(mViewModel)
        }
    }
}

